const mongoose = require('mongoose');
const Drawing = require('./schema/drawing');

/**
 * 连接mongodb
 * @param {*} options
 * @returns
 */
module.exports = async function MongoDatabase(options) {
	const { url } = options;

	return await mongoose.connect(url).then(() => {
		const mongoDB = mongoose.connection;

		[Drawing].forEach(item => {
			item(mongoose);
		});

		const MongoModel = {
			Drawing: mongoose.model('drawings')
		};

		mongoDB.on('error', (e) => {
			console.error(e);
		});

		return MongoModel;
	});
};